package com.boatAnchorAssistant.service.repository.impl;

import com.boatAnchorAssistant.pojo.entity.DataDictionaryType;
import com.boatAnchorAssistant.service.mapper.DataDictionaryTypeMapper;
import com.boatAnchorAssistant.service.repository.in.DataDictionaryTypeRepository;
import nirvana.core.exceptions.BizException;
import nirvana.core.repository.Repository;
import org.springframework.stereotype.Service;
import com.boatAnchorAssistant.pojo.dto.dataDictionaryType.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDate;

/**
 * 【描 述】：
 * 【环 境】：J2SE
 * 【详 细】：
 *
 */
@Service
public class DataDictionaryTypeRepositoryImpl extends Repository<DataDictionaryTypeMapper, DataDictionaryType> implements DataDictionaryTypeRepository {

    private final DataDictionaryTypeMapper mapper;

    public DataDictionaryTypeRepositoryImpl(DataDictionaryTypeMapper mapper) {
        this.mapper = mapper;
    }


    @Override
    public DataDictionaryType createToBefore(DataDictionaryType dataDictionaryType) {
        String name = dataDictionaryType.getName();
        String type = dataDictionaryType.getType();


        DataDictionaryType po = null;
        DataDictionaryType byPo = null;


        po = new DataDictionaryType();
        po.setName(name);
        byPo = getByEntity(po);

        if (byPo != null) {
            throw new BizException("数据字典类型名称不能重复");
        }

        po = new DataDictionaryType();
        po.setType(type);
        byPo = getByEntity(po);

        if (byPo != null) {
            throw new BizException("数据字典类型类型不能重复");
        }


        return super.createToBefore(dataDictionaryType);
    }


    @Override
    public DataDictionaryType editToBefore(DataDictionaryType dataDictionaryType) {

        Long id = dataDictionaryType.getId();
        String name = dataDictionaryType.getName();
        String type = dataDictionaryType.getType();


        DataDictionaryType po = null;
        DataDictionaryType byPo = null;


        po = new DataDictionaryType();
        po.setName(name);
        byPo = getByEntity(po);

        if (byPo != null && !id.equals(byPo.getId())) {
            throw new BizException("数据字典类型名称不能重复");
        }

        po = new DataDictionaryType();
        po.setType(type);
        byPo = getByEntity(po);

        if (byPo != null && !id.equals(byPo.getId())) {
            throw new BizException("数据字典类型类型不能重复");
        }

        return super.editToBefore(dataDictionaryType);
    }


    /**
     * 搜索数据字典类型
     * @param dto
     * @return
     */
    @Override
    public Page<DataDictionaryType> selectDataDictionaryType(SelectDataDictionaryTypeDto dto) {
        LocalDate endDate = dto.getEndDate();
        if (endDate!=null){
            dto.setEndDate(endDate.plusDays(1));
        }
        return mapper.selectDataDictionaryType(dto);
    }

}